<template>
	<view class="Productlibrary">
		<view class="box">
			<u-cell-item title="类型" required :value='dataForm.typeName' @click='typeshow=true'></u-cell-item>
			<u-cell-item title="单号" :value='dataForm.number' :arrow='false'></u-cell-item>
			<u-cell-item title="仓库" required :value='dataForm.name' @click='addressshow=true'></u-cell-item>
			<u-cell-item title="日期" required :value='dataForm.date_time' @click='timeshow=true'></u-cell-item>
			<u-cell-item title="数量" required :arrow='false'>
				<view class="" slot='right-icon'>
					<u-number-box v-model="num" :max="type==1?product.stock_num:999999"></u-number-box>
				</view>
			</u-cell-item>
		</view>
		<view class="remark">
			<view class="">备注</view>
			<textarea placeholder="请输入备注" v-model="dataForm.remark"/>
		</view>
		<view class="btn" @click="sumbit">确定{{typeName}}</view>
		<u-picker v-model="typeshow" mode="selector" :range='type==1?outstoctList:instoctList' range-key='label'  @confirm="choosetype"></u-picker>
		<u-picker v-model="addressshow" mode="selector" :range='addressList' range-key='name' @confirm="chooseaddress"></u-picker>
		<u-picker v-model="timeshow" mode="time" @confirm="choosetime"></u-picker>
	</view>
</template>

<script>
	import {
		adminPost,
		GetQueryDataList
	} from '@/common/http.request.js'
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				dataForm: {
					number: '',
					type:'',
					typeName:'请选择',
					name:'请选择',
					remark:'',
					date_time:'请选择'
				},
				num:0,
				type: '',
				addressID:'',
				typeName: '',
				product: '',
				typeshow:false,
				addressshow:false,
				timeshow:false,
				addressList:[],
				instoctList: [{
						label: '初始入库',
						value: '1'
					}, {
						label: '采购入库',
						value: '2'
					}, {
						label: '销售退货入库',
						value: '3'
					},
					{
						label: '其他入库',
						value: '4'
					}, {
						label: '盘盈入库',
						value: '7'
					}
				],
				outstoctList: [{
					label: '销售出库',
					value: '5'
				}, {
					label: '采购退货出库',
					value: '6'
				}, {
					label: '盘亏出库',
					value: '8'
				}, {
					label: '其他出库',
					value: '9'
				}]
			}
		},
		computed: {
			...mapState(['vuex_adminuser'])
		},
		onLoad(options) {
			
			if (options.type) {
				this.type = options.type
			}
			try{
				let page = getCurrentPages()
				this.product = page[page.length-2].$vm.dataList[options.index]
			}catch{}
			this.initPage()
		},
		methods: {
			initPage(){
				this.typeName = this.type == '1' ? '出库' : '入库'
				uni.setNavigationBarTitle({
				    title: this.type == '1' ?'产品出库':'产品入库'
				});
				let dat = new Date()
				this.dataForm.number =
					'' +
					dat.getFullYear() +
					this.timeAdd0(dat.getMonth() + 1) +
					this.timeAdd0(dat.getDate()) +
					Math.floor(Math.random() * 1000 + 1000)
				GetQueryDataList('e_product_warehouse',1,100,{fk_business_id:this.vuex_adminuser.pk_id, status: 1}).then(res=>{
					this.addressList = res.data
				})
			},
			timeAdd0(str) {
				if (str < 10) {
					str = '0' + str
				}
				return str
			},
			chooseaddress(val){
				this.dataForm.name = this.addressList[val[0]].name
				this.addressID = this.addressList[val[0]].pk_id
			},
			choosetype(val){
				this.dataForm.type = this.type =='1'?this.outstoctList[val[0]].value:this.instoctList[val[0]].value
				this.dataForm.typeName = this.type =='1'?this.outstoctList[val[0]].label:this.instoctList[val[0]].label
			},
			choosetime(val){
				this.dataForm.date_time = val.year+'-'+val.month+'-'+val.day
			},
			sumbit() {
				if(!this.dataForm.type){
					return this.$toast('类型不能为空')
				}else if(!this.addressID){
					return this.$toast('仓库不能为空')
				}else if(this.dataForm.date_time == '请选择'){
					return this.$toast('日期不能为空')
				}else if(!this.num){
					return this.$toast('数量不能为空')
				}
				delete this.dataForm.typeName
				let json = [{
						fk_warehouse_id:this.addressID,
						fk_stock_id:this.product.pk_id,
						fk_order_pro_id:0,
						remark:this.product.remark,
						num:this.num
					}]
				let params = {
					pk_id:this.vuex_adminuser.pk_id,
					...this.dataForm,
					json_detailed:JSON.stringify(json)
				}
				adminPost('AddProductStock',params).then(res=>{
					this.$linkTo(`/package/system/erp/stock/InventoryList?type=${this.type}`,'redirectTo')
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang='scss' scoped>
	.Productlibrary {
		margin-top: 20rpx;

		.box {
			background-color: #fff;
		}

		.remark {
			margin-top: 20rpx;
			padding: 30rpx;
			background-color: #fff;

			textarea {
				width: 100%;
				padding-top: 10rpx;
			}
		}

		.btn {
			margin: 120rpx 40rpx 0;
			width: 670rpx;
			background-color: #3297ef;
			color: #fff;
			line-height: 80rpx;
			text-align: center;
			border-radius: 10rpx;
		}
	}
</style>
